package com.dht.xiao.jianye.activity_work_clothes;

import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.dht.xiao.jianye.R;
import com.dht.xiao.jianye.adapter.BedGetAdapter;
import com.dht.xiao.jianye.adapter.WorkQueryAdapter;
import com.dht.xiao.jianye.base.BaseActivity;
import com.dht.xiao.jianye.bean.DeviceStaffBean;
import com.dht.xiao.jianye.bean.QueryCaseBean;
import com.dht.xiao.jianye.bean.SimpleBean;
import com.dht.xiao.jianye.bean.newdata.OfficeBean;
import com.dht.xiao.jianye.http.MainHttp;
import com.dht.xiao.jianye.utils.OpenManager;
import com.licheedev.myutils.LogPlus;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dlc.commonlibrary.okgo.rx.OkObserver;
import cn.dlc.commonlibrary.ui.widget.CommonItemView;
import cn.dlc.commonlibrary.ui.widget.TitleBar;
import cn.dlc.commonlibrary.ui.widget.popup.CommonPopupList;
import cn.dlc.commonlibrary.utils.ToastUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class WorkQueryActivity extends BaseActivity {
    @BindView(R.id.mTitleBar)
    TitleBar mTitleBar;

    @BindView(R.id.tv_person)
    TextView tvPerson;
    @BindView(R.id.drop_person)
    FrameLayout dropPerson;
    @BindView(R.id.item_person)
    CommonItemView itemPerson;
    @BindView(R.id.btn_search)
    Button btnSearch;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.drop_number)
    FrameLayout dropNumber;
    @BindView(R.id.item_number)
    CommonItemView itemNumber;

    String ANDROID_ID;

    private CommonPopupList mPersonPopup, mNumberPopup;
    private List<DeviceStaffBean.DataBean> mPersonList;
    private List<String> mNumberList = new ArrayList<>();
    private String mPerson_id = "",mNumber_id = "";
    WorkQueryAdapter mRecyclerAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_work_query;
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_query);
        ButterKnife.bind(this);
        initView();
        initRecyclerView();
        getMachineCode();
    }

    private void getMachineCode() {
        ANDROID_ID =
                Settings.System.getString(getContentResolver(), Settings.System.ANDROID_ID);
        if (TextUtils.isEmpty(ANDROID_ID)) {
            ANDROID_ID = Build.SERIAL;
        }
        LogPlus.e("ANDROID_ID =" + ANDROID_ID);
        ANDROID_ID = "909203d36a1a82fb";
        //ANDROID_ID = "6f4b9da9f38b5566";
        // seriesNum.setText(ANDROID_ID);
        deviceStaff();
        for (int i = 1;i < 33;i++){
            mNumberList.add(i+"");
        }
        mNumberPopup.setData(mNumberList);
    }
    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        mRecyclerAdapter = new WorkQueryAdapter();
        mRecyclerView.setAdapter(mRecyclerAdapter);
    }

    private void initView() {

        mNumberPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(dropNumber)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        tvNumber.setText(item);
                        if (mNumberList != null) {
                            mNumber_id = mNumberList.get(position);
                        }

                    }
                })
                .build();

        mPersonPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(dropPerson)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        tvPerson.setText(item);
                        if (mPersonList != null) {
                            mPerson_id = mPersonList.get(position).id;
                            tvNumber.setText("");
                            mNumber_id = "";
                        }

                    }
                })
                .build();

    }

    @OnClick({R.id.drop_person, R.id.drop_number, R.id.btn_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.drop_person:
                mPersonPopup.showAsDropDown(view);
                break;
            case R.id.drop_number:
                mNumberPopup.showAsDropDown(view);
                break;
            case R.id.btn_search:
                queryCase();
                break;
        }
    }

    private void queryCase() {
        MainHttp.get().queryCase(ANDROID_ID,mPerson_id,mNumber_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<QueryCaseBean>() {
                    @Override
                    public void onSuccess(QueryCaseBean officeBean) {

                        if (officeBean.data != null && officeBean.data.size() == 0){
                            showOneToast("数据为空");
                        }

                        mRecyclerAdapter.setNewData(officeBean.data);
                    }

                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(), message);
                    }
                });
    }
    private void deviceStaff() {
        MainHttp.get().deviceStaff(ANDROID_ID)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<DeviceStaffBean>() {
                    @Override
                    public void onSuccess(DeviceStaffBean officeBean) {

                        List<String> datas = new ArrayList<>();
                        mPersonList = officeBean.data;
                        for (DeviceStaffBean.DataBean bean : officeBean.data) {
                            datas.add(bean.name);
                        }
                        mPersonPopup.setData(datas);
                    }

                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(), message);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
